package com.eu.mybatis.config;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.eu.mybatis.partition.strategy.PartitionStrategy;

public class PartitionConfig {
	
	private final static Map<String, PartitionStrategy> strategies = new HashMap<String, PartitionStrategy>();
	
	private final static Set<String> tableNames = new HashSet<String>();
	
	private final static Map<String, Set<TableConfig>> tableConfigMap = new HashMap<String, Set<TableConfig>>();

	public static void addStrategy(String key, PartitionStrategy strategy) {
		strategies.put(key.toUpperCase(), strategy);
	}

	public static void addTableName(String tableName) {
		String nameUpper = tableName.toUpperCase();
		tableNames.add(" " + nameUpper + " ");
		tableNames.add(" " + nameUpper + "(");
		tableNames.add("," + nameUpper + " ");
		tableNames.add("," + nameUpper + ",");
		
		Set<TableConfig> values = new HashSet<TableConfig>();
		values.add(new TableConfig(" ", tableName, " ", " "));
		values.add(new TableConfig(" ", tableName, "(", "\\("));
		values.add(new TableConfig(",", tableName, " ", " "));
		values.add(new TableConfig(",", tableName, ",", ","));
		
		tableConfigMap.put(tableName, values);
	}

	public static PartitionStrategy getStrategy(String key) {
		return strategies.get(key.toUpperCase());
	}

	public static boolean strategyDefined() {
		return strategies.size() > 0;
	}

	public static Set<String> getTableNames() {
		return tableNames;
	}

	public static Map<String, Set<TableConfig>> getTableConfigMap() {
		return tableConfigMap;
	}
	
	public static Set<TableConfig> getTableConfigs(String tableName) {
		return tableConfigMap.get(tableName);
	}
	
	public static Set<String> getConvertTables() {
		return tableConfigMap.keySet();
	}
	
}